﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using Workflow.Alerts.DAL;

namespace Workflow.Alerts
{
    public class PersonDtlsPresenter : Presenter
    {
        #region members

        private readonly PersonDAL model;
        private readonly IPersonView view;

        #endregion

        #region constructor
        public PersonDtlsPresenter(IPersonView _view)
            : this(_view, null)
        { }

        public PersonDtlsPresenter(IPersonView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IPersonView>();
            view.OnPageLoad += view_OnLoad;
            view.OnDataUpdate += view_Update;
            model = new PersonDAL();
        }

        #endregion

        #region private methods

        private void view_OnLoad()
        {
            var filter = new WorkflowFilter
            {
                UserId = view.Id
            };

            var filterSql = new WorkflowFilterSql(filter);

            EstateDataSet.PersonDataTable profileTable = model.GetPersonDetails(filterSql);

            if (profileTable.Rows.Count == 1)
            {
                EstateDataSet.PersonRow profile = profileTable[0];
                view.FirstName = profile.firstname;
                view.LastName = profile.lastname;
                view.Phone1 = profile.Phone1;
                view.Phone2 = profile.Phone2;
                view.Email = profile.email;
                view.Notifications = profile.notification;
                view.Confirmations = profile.confirmation;
                view.Login = profile.login;
                view.Password = profile.password;
            }
        }

        private bool view_Update()
        {
            var filter = new WorkflowFilter
            {
                FirstName = view.FirstName,
                LastName = view.LastName,
                Phone1 = view.Phone1,
                Phone2 = view.Phone2,
                Email = view.Email,
                Notification = view.Notifications,
                Confirmation = view.Confirmations,
                Login = view.Login,
                Password = view.Password,
                UserId = view.Id
                
            };

            WorkflowFilterSql filterSql = new WorkflowFilterSql(filter);

            string exceptionField = string.Empty;
            if (!model.ValidateUser(filterSql, ref exceptionField))
            {
                view.ExceptionField = exceptionField;
                return false;
            }

            filter.NetworkOperatorId = view.NetworkOperatorId;
            filterSql = new WorkflowFilterSql(filter);

            if (view.Id.HasValue)
            {
                model.UpdatePerson(filterSql);
            }
            else
            {
                model.CreateNetworkOperatorPerson(filterSql);
            }

            if (Sitecore.Context.Item.Name == "MyProfile")
            {
                CurrentUser.FirstName = view.FirstName;
                CurrentUser.LastName = view.LastName;
                CurrentUser.Email = view.Email;
                CurrentUser.Confirmation = view.Confirmations;
                CurrentUser.Notification = view.Notifications;
            }

            return true;
        }

        #endregion
    }
}
